package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParamListBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.mall.bo.BrandBaseBo;
import com.cskaoyan.bean.admin.mall.bo.BrandCreateBo;
import com.cskaoyan.bean.admin.mall.bo.BrandUpdateBo;
import com.cskaoyan.bean.admin.mall.vo.BrandCreateVo;
import com.cskaoyan.bean.admin.mall.vo.BrandUpdateVo;
import com.cskaoyan.bean.pojo.Brand;
import com.cskaoyan.service.admin.BrandService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: project-two
 * @description: committed by yuhui
 * @author: YuHui
 * @create: 2021-08-13 16:21
 **/
@RestController
@RequestMapping("admin/brand")
public class BrandController {

    @Autowired
    BrandService brandService;

    /**
     * 品牌制造商的刷新
     * @param baseParamListBo
     * @return
     */
    @RequiresPermissions(value = {"*", "admin:brand:list"}, logical = Logical.OR)
    @GetMapping("/list")
    public BaseRespVo list(BaseParamListBo baseParamListBo){

        BaseRespData<Brand> baseRespData = brandService.list(baseParamListBo);
        return BaseRespVo.ok(baseRespData);
    }


    /**
     * 品牌制造商的新增
     * @param brandCreateBo
     * @return
     */
    @RequiresPermissions(value = {"*", "admin:brand:create"}, logical = Logical.OR)
    @RequestMapping("/create")
    public BaseRespVo create(@RequestBody BrandCreateBo brandCreateBo){
        BrandCreateVo brandCreateVo = brandService.create(brandCreateBo);
        return BaseRespVo.ok(brandCreateVo);
    }


    /**
     * 品牌制造商的删除
     */
    @RequiresPermissions(value = {"*", "admin:brand:delete"}, logical = Logical.OR)
    @PostMapping("/delete")
   public BaseRespVo delete(@RequestBody BrandBaseBo brandBaseBo){
        brandService.delete(brandBaseBo);
        return BaseRespVo.ok();
    }


    /**
     * 品牌制造商的更新
     * @param brandUpdateBo
     * @return
     */
    @RequiresPermissions(value = {"*", "admin:brand:update"}, logical = Logical.OR)
    @PostMapping("update")
    public BaseRespVo update(@RequestBody BrandUpdateBo brandUpdateBo) {

        if (brandUpdateBo.getFloorPrice() == null) {
            return BaseRespVo.fail();
        }

        Double d = brandUpdateBo.getFloorPrice().doubleValue();
        if (d < 0) {

            return BaseRespVo.fail();
        }
        BrandUpdateVo brandUpdateVo = brandService.update(brandUpdateBo);
        return BaseRespVo.ok(brandUpdateVo);
    }

}
